﻿Imports System
Imports System.ComponentModel
Imports System.Drawing
Imports System.Runtime.CompilerServices
Imports System.Windows.Forms
Imports System.Windows.Forms.Design
Imports System.Workflow.Activities.Rules

Namespace System.Workflow.Activities.Rules.Design
    Friend Class RenameRuleObjectDialog
        Inherits Form
        ' Methods
        Public Sub New(ByVal serviceProvider As IServiceProvider, ByVal oldName As String, ByVal nameValidator As NameValidatorDelegate, ByVal parent As BasicBrowserDialog)
            If (oldName Is Nothing) Then
                Throw New ArgumentNullException("oldName")
            End If
            If (serviceProvider Is Nothing) Then
                Throw New ArgumentNullException("serviceProvider")
            End If
            If (nameValidator Is Nothing) Then
                Throw New ArgumentNullException("nameValidator")
            End If
            Me.serviceProvider = serviceProvider
            Me.name = oldName
            Me.nameValidator = nameValidator
            Me.parent = parent
            Me.InitializeComponent
            Me.ruleNameTextBox.Text = oldName
            Me.Text = parent.RenameTitleText
            Me.newNamelabel.Text = parent.NewNameLabelText
            MyBase.Icon = Nothing
            Dim service As IUIService = DirectCast(Me.serviceProvider.GetService(GetType(IUIService)), IUIService)
            If (Not service Is Nothing) Then
                Me.Font = DirectCast(service.Styles.Item("DialogFont"), Font)
            End If
        End Sub

        Private Shared Function DetermineOptions(ByVal sender As Object) As MessageBoxOptions
            Dim options As MessageBoxOptions = 0
            Dim parent As Control = [TryCast](sender,Control)
            Dim inherit As RightToLeft = RightToLeft.Inherit
            Do While ((inherit = RightToLeft.Inherit) AndAlso (Not parent Is Nothing))
                inherit = parent.RightToLeft
                parent = parent.Parent
            Loop
            If (inherit = RightToLeft.Yes) Then
                options = (MessageBoxOptions.RtlReading Or MessageBoxOptions.RightAlign)
            End If
            Return options
        End Function

        Protected Overrides Sub Dispose(ByVal disposing As Boolean)
            If (disposing AndAlso (Not Me.components Is Nothing)) Then
                Me.components.Dispose
            End If
            MyBase.Dispose(disposing)
        End Sub

        Private Sub InitializeComponent()
            Dim manager As New ComponentResourceManager(GetType(RenameRuleObjectDialog))
            Me.cancelButton = New Button
            Me.okButton = New Button
            Me.newNamelabel = New Label
            Me.ruleNameTextBox = New TextBox
            Me.okCancelTableLayoutPanel = New TableLayoutPanel
            Me.okCancelTableLayoutPanel.SuspendLayout
            MyBase.SuspendLayout
            manager.ApplyResources(Me.cancelButton, "cancelButton")
            Me.cancelButton.CausesValidation = False
            Me.cancelButton.DialogResult = DialogResult.Cancel
            Me.cancelButton.Name = "cancelButton"
            AddHandler Me.cancelButton.Click, New EventHandler(AddressOf Me.OnCancel)
            manager.ApplyResources(Me.okButton, "okButton")
            Me.okButton.DialogResult = DialogResult.OK
            Me.okButton.Name = "okButton"
            AddHandler Me.okButton.Click, New EventHandler(AddressOf Me.OnOk)
            manager.ApplyResources(Me.newNamelabel, "newNamelabel")
            Me.newNamelabel.Name = "newNamelabel"
            Me.newNamelabel.UseCompatibleTextRendering = True
            manager.ApplyResources(Me.ruleNameTextBox, "ruleNameTextBox")
            Me.ruleNameTextBox.Name = "ruleNameTextBox"
            manager.ApplyResources(Me.okCancelTableLayoutPanel, "okCancelTableLayoutPanel")
            Me.okCancelTableLayoutPanel.Controls.Add(Me.okButton, 0, 0)
            Me.okCancelTableLayoutPanel.Controls.Add(Me.cancelButton, 1, 0)
            Me.okCancelTableLayoutPanel.Name = "okCancelTableLayoutPanel"
            MyBase.AcceptButton = Me.okButton
            manager.ApplyResources(Me, "$this")
            MyBase.CancelButton = Me.cancelButton
            MyBase.Controls.Add(Me.okCancelTableLayoutPanel)
            MyBase.Controls.Add(Me.ruleNameTextBox)
            MyBase.Controls.Add(Me.newNamelabel)
            MyBase.FormBorderStyle = FormBorderStyle.FixedDialog
            MyBase.MaximizeBox = False
            MyBase.MinimizeBox = False
            MyBase.Name = "RenameRuleObjectDialog"
            MyBase.ShowInTaskbar = False
            Me.okCancelTableLayoutPanel.ResumeLayout(False)
            Me.okCancelTableLayoutPanel.PerformLayout
            MyBase.ResumeLayout(False)
            MyBase.PerformLayout
        End Sub

        Private Sub OnCancel(ByVal sender As Object, ByVal e As EventArgs)
            MyBase.DialogResult = DialogResult.Cancel
            MyBase.Close
        End Sub

        Private Sub OnOk(ByVal sender As Object, ByVal e As EventArgs)
            Dim text As String = Me.ruleNameTextBox.Text
            If ([text].Trim.Length = 0) Then
                Dim emptyNameErrorText As String = Me.parent.EmptyNameErrorText
                Dim service As IUIService = DirectCast(Me.serviceProvider.GetService(GetType(IUIService)), IUIService)
                If (Not service Is Nothing) Then
                    service.ShowError(emptyNameErrorText)
                Else
                    MessageBox.Show(emptyNameErrorText, Messages.InvalidConditionNameCaption, MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1, RenameRuleObjectDialog.DetermineOptions(sender))
                End If
                MyBase.DialogResult = DialogResult.None
            ElseIf ((Me.name <> [text]) AndAlso Not Me.nameValidator.Invoke([text])) Then
                Dim duplicateNameErrorText As String = Me.parent.DuplicateNameErrorText
                Dim service2 As IUIService = DirectCast(Me.serviceProvider.GetService(GetType(IUIService)), IUIService)
                If (Not service2 Is Nothing) Then
                    service2.ShowError(duplicateNameErrorText)
                Else
                    MessageBox.Show(duplicateNameErrorText, Messages.InvalidConditionNameCaption, MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1, RenameRuleObjectDialog.DetermineOptions(sender))
                End If
                MyBase.DialogResult = DialogResult.None
            Else
                Me.name = [text]
                MyBase.DialogResult = DialogResult.OK
                MyBase.Close
            End If
        End Sub


        ' Properties
        Public ReadOnly Property RuleObjectName As String
            Get
                Return Me.name
            End Get
        End Property


        ' Fields
        Private cancelButton As Button
        Private components As IContainer
        Private name As String
        Private nameValidator As NameValidatorDelegate
        Private newNamelabel As Label
        Private okButton As Button
        Private okCancelTableLayoutPanel As TableLayoutPanel
        Private parent As BasicBrowserDialog
        Private ruleNameTextBox As TextBox
        Private serviceProvider As IServiceProvider

        ' Nested Types
        Public Delegate Function NameValidatorDelegate(ByVal name As String) As Boolean
    End Class
End Namespace

